package cn.icanci.ladybird.config;

import cn.icanci.ladybird.interceptor.AdminLoginInterceptor;
import cn.icanci.ladybird.interceptor.CorsInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: icanci
 * @date: Created in 2020/9/29 14:58
 * @classAction: 处理拦截器的配置类
 * -
 * - 不使用权限就可以访问的：/api/v1/pub
 * - 需要使用权限访问: /api/v1/pri/
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Bean
    AdminLoginInterceptor adminLoginInterceptor() {
        return new AdminLoginInterceptor();
    }


    @Bean
    CorsInterceptor corsInterceptor() {
        return new CorsInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 拦截全部，并且放行
        registry.addInterceptor(corsInterceptor()).addPathPatterns("/**");

        // 拦截管理员请求 /api/v1/pri/admin
        registry.addInterceptor(adminLoginInterceptor())
                // 拦截所有的路径
                .addPathPatterns("/api/v1/pri/admin/*/**")
                // 不拦截登录、注册 请求
                .excludePathPatterns("/api/v1/pri/admin/login",
                        "/api/v1/pri/admin/login-up",
                        "/api/v1/pri/admin/logout",
                        "/api/v1/pri/admin/403"
                );

        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
